package com.paas.form.core.element.base;

import com.gencode.util.CodeBuildNew;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.button.Button;
import com.paas.form.core.element.FieldElement;
import com.paas.form.core.element.constants.SearchConstant;
import com.paas.form.core.element.field.SearchListModel;
import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.control.FormItemFactory;
import com.paas.form.core.element.jsContent.ScriptContent;
import lombok.Data;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Data
public class Search implements FieldElement {
    public List<Button> buttonList;
    public List<SearchListModel> fields;

    private Map<String, Object> map = new HashMap<>();


    public Search(String title,List<Button> buttonList,List<SearchListModel> fields){
        this.buttonList = buttonList;
        this.fields= fields;
        this.map.put("colSpan",8);
        this.map.put("formObject","searchForm");
        this.map.put("searchList",fields);
        this.map.put("title",title);
    }





    private String resetVue() {
        return "";
    }


    private String buildSearchFormHtml() {
        if (fields == null || fields.size() == 0) {
            return "";
        }
        StringBuffer searchFormHtml =new StringBuffer();
        //添加标题及按钮列
        searchFormHtml.append(SearchConstant.VM_SEARCH_TITLE_HEAD);
//        if(BeanUtil.isNotEmpty(buttonList)&&buttonList.size()>0){
//            for (Button button:buttonList) {
//                map.put("button",button);
//                String buttonString = CodeBuildNew.getCodeString(map, SearchConstant.VM_SEARCH_TITLE_BUTTON);
//                searchFormHtml.append(buttonString);
//            }
//
//        }

        searchFormHtml.append(SearchConstant.VM_SEARCH_TITLE_DEFAULT_BUTTON);
        searchFormHtml.append(SearchConstant.VM_SEARCH_TITLE_END);



        searchFormHtml.append(SearchConstant.VM_SEARCH_FORM_START);

        for (SearchListModel searchListModel :fields) {
            String itemString = buildFormItemHtml(searchListModel);
            //加6tab缩进
            itemString ="                        " +itemString;
            searchFormHtml.append(itemString);
        }
        searchFormHtml.append(SearchConstant.VM_SEARCH_FORM_END);

        return CodeBuildNew.getCodeString(map, searchFormHtml.toString());
    }

    /**
     * 描述:构建一个查询字段独享
     *
     * @param searchListModel
     * @return
     */
    private String buildSearchItemHtml(SearchListModel searchListModel) {
        FormItem formItem = FormItemFactory.create(searchListModel,"search");
        return formItem.makeVue();
    }

    private ScriptContent buildSearchItemJs(SearchListModel searchListModel) {
        FormItem formItem = FormItemFactory.create(searchListModel,"search");
        return formItem.makeJs();
    }




    //获取到查询表单的一个<el-col>部分
    private String buildFormItemHtml(SearchListModel searchListModel){
        StringBuffer searchItemHtml =new StringBuffer();

        searchItemHtml.append("\n" + SearchConstant.VM_SEARCH_FORM_COLStart);
        searchItemHtml.append("\n" + String.join("", Collections.nCopies(9, "\t")) +  buildSearchItemHtml(searchListModel));
        map.put("field", searchListModel);

        //替换<el-col>部分的$参数
        searchItemHtml.append(SearchConstant.VM_SEARCH_FORM_COLEND);

        String CodeString = CodeBuildNew.getCodeString(map,searchItemHtml.toString());

        return CodeString;

    }


    private ScriptContent buildSearchJs(){
        if (fields == null || fields.size() == 0) {
            return null;
        }
        ScriptContent scriptContent = new ScriptContent();
        String dataString = CodeBuildNew.getCodeString(map,SearchConstant.VM_SEARCH_JS_DATA);
        //        scriptContent.setComponents();
//        scriptContent.setProps();
        scriptContent.setData(dataString);
//        scriptContent.setMethods();


        for (SearchListModel searchListModel :fields) {
            map.put("field", searchListModel);
            ScriptContent itemScript = buildSearchItemJs(searchListModel);

            if(BeanUtil.isNotEmpty(itemScript)){
                //把个item模块的js/data/methods 拼接
                if(StringUtil.isNotEmpty(itemScript.getComponents())){
                    String newComStr = CodeBuildNew.getCodeString(map,itemScript.getComponents());

                    scriptContent.setComponents(scriptContent.getComponents() + "\n" +newComStr);
                }
                if(StringUtil.isNotEmpty(itemScript.getData())){
                    String newDataStr = CodeBuildNew.getCodeString(map,itemScript.getData());
                    scriptContent.setData(scriptContent.getData() + "\n" +newDataStr);
                }
                if(StringUtil.isNotEmpty(itemScript.getMethods())){
                    String newMethodsStr = CodeBuildNew.getCodeString(map,itemScript.getMethods());
                    scriptContent.setMethods(scriptContent.getMethods() + "\n" + newMethodsStr);
                }
                if(StringUtil.isNotEmpty(itemScript.getProps())){
                    String newPropsStr = CodeBuildNew.getCodeString(map,itemScript.getProps());
                    scriptContent.setProps(scriptContent.getProps() + "\n" + newPropsStr);
                }
            }



        }


        //添加查询按钮相关js
        //







        return scriptContent;



    }



    @Override
    public String makeVue() {


        return buildSearchFormHtml();
    }

    @Override
    public ScriptContent makeJs() {
        return buildSearchJs();
    }
}
